#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>

int cmp(const void* p1, const void* p2)
{
	if (*(int*)p1 >* (int*)p2)
		return 1;
	else if (*(int*)p1 <* (int*)p2)
		return -1;
	else
		return 0;
}
void print(int* arr, int x)
{
	int i = 0;
	for (i = 0; i < x; i++)
	{
		printf("%d ", arr[i]);
	}
	printf("\n");

}
struct stu
{
	int age;
	char name[20];
};
int cmp_name(const void* p1, const void* p2)
{
	return strcmp(((struct stu*)p1)->name, ((struct stu*)p2)->name);
}
int cmp_age(const void* p1, const void* p2)
{
	return ((struct stu*)p1)->age-((struct stu*)p2)->age;
}
void sawp(char* buf1, char* buf2, size_t width, int* exchange)
{
	int i = 0;
	char tmp = 0;
	for (i = 0; i < width; i++)
	{
		tmp = *buf1;
		*buf1 = *buf2;
		*buf2 = tmp;
		buf1++;
		buf2++;
		
	}
	*exchange = 1;
}
void my_qsort(void* base, size_t num, size_t width, int (*cmp)(const void* p1, const void* p2))
{
	int i = 0;
	for (i = 0; i < num - 1; i++)
	{
		int j = 0;
		int exchange = 0;
		for (j = 1; j <num- i; j++)
		{
			if (cmp(((char*)base + (j - 1) * width), (char*)base + (j)*width) > 0)
			{
				sawp(((char*)base + (j - 1) * width), ((char*)base + j*width),width,&exchange);
			}
		}
		if (exchange == 0)
		{
			return;
		}
	}
}
void test1()
{
	struct stu p2[3] = { {1,"ad"},{2,"daf"},{3,"cafaf"}};
	int arr[] = { 2,3,4,1,6,8,4,6,4,6 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	int sz1 = sizeof(p2) / sizeof(p2[0]);
	my_qsort(arr, sz, sizeof(arr[0]), cmp);
	my_qsort(p2, sz1, sizeof(p2[0]), cmp_name);
	my_qsort(p2, sz1, sizeof(p2[0]), cmp_age);
 	print(arr, sz);
}
int main()
{
	test1();
	return 0;
}